package org.whatever.aha.zjut.platform.controller;

import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.whatever.aha.zjut.base.config.TencentCosConfig;
import org.whatever.aha.zjut.base.dto.AjaxResult;
import org.whatever.aha.zjut.platform.service.TencentCosService;
import org.whatever.aha.zjut.platform.vo.CosPolicyVo;

/**
 * @author Vc
 * @version 1.0
 * @Desc COS相关请求
 * @date 2021/10/08 23:51
 */
@RestController
@Api(tags = "Cos相关api")
@RequiredArgsConstructor
@RequestMapping("/cos")
public class CosController {
    final TencentCosService cosService;
    final TencentCosConfig tencentCosConfig;

    /**
     * 获取COS公开资源上传签名
     *
     * @param filename 待上传文件名
     */
    @GetMapping("/sign/upload")
    public AjaxResult<CosPolicyVo> signUpload(@RequestParam("filename") String filename) {
        int userId = StpUtil.getLoginIdAsInt();
        return AjaxResult.SUCCESS(cosService.signPut( "/user_files/" + userId + "/" + filename));
    }

    /**
     * 获取COS公开资源下载签名
     *
     * @param filename
     * @return
     */
    @GetMapping("/sign/download")
    public AjaxResult<CosPolicyVo> signDownload(@RequestParam("filename") String filename) {
        int userId = StpUtil.getLoginIdAsInt();
        return AjaxResult.SUCCESS(cosService.signDownload("/user_files/" + userId + filename));
    }

    /**
     * 获取COS公开资源删除签名
     *
     * @param filename
     * @return
     */
    @GetMapping("/sign/delete")
    public AjaxResult<CosPolicyVo> signDelete(@RequestParam("filename") String filename) {
        int userId = StpUtil.getLoginIdAsInt();
        return AjaxResult.SUCCESS(cosService.signDelete("/user_files/" + userId + filename));
    }
}
